/**
 * @author yuyangyang
 * @time 2015/3/11
 */
var _ = require('underscore');
var logger = require('../utils/logger').getLogger('default');
var meetService = require('../services/meet');
var categoryService = require('../services/sport');
var userService = require('../services/user');
var joinService = require('../services/join');

module.exports = {
    mapping: '/meet',
    get: {
        '/:sportId': function (req, res) {
            meetService.queryBySport({sport: req.params.sportId}, function (err, data) {
                res.json(data);
            });
        },

        '/detail/:meetId': function (req, res) {
            meetService.queryById(req.params.meetId, function (err, meet) {
                joinService.queryBySport(req.session.userId,{meet: req.params.meetId}, function (err, joinList) {
                    res.json({
                        meet: meet,
                        joinList: joinList
                    });
                });
            });
        },
        '/:meetId': function (req, res) {
            meetService.queryById(req.params.meetId, function (err, meet) {
                    userService.queryById(meet.creater, function (err, user) {
                        categoryService.queryById(meet.sport, function (err, category) {
                            meet.creater = user;
                            meet.sport = category;
                            res.render('meet/index', {
                                meet: meet
                            });
                        });
                    });

                }
            )
            ;
        }
    },
    post: {
        '/new': function (req, res) {
            meetService.addMeet(_.extend({creater: req.session.userId}, req.body), function (err, data) {
                res.json(data);
            });
        }
    }
}